$(function(e){
    var t;
    var firsttel ='';
    
//    document.addEventListener('DOMContentLoaded', function (event) {
//        // chrome 浏览器直接加上下面这个样式就行了，但是ff不识别
//        document.body.style.zoom = 'reset';
//        document.addEventListener('keydown', function (event) {
//            if ((event.ctrlKey === true || event.metaKey === true)
//            && (event.which === 61 || event.which === 107
//                || event.which === 173 || event.which === 109
//                || event.which === 187  || event.which === 189))
//                {
//                   event.preventDefault();
//                }
//        }, false);
//        document.addEventListener('mousewheel DOMMouseScroll', function (event) {
//            if (event.ctrlKey === true || event.metaKey) {
//                event.preventDefault();
//            }
//        }, false);
//    }, false);

    // pin设置获取输入值
    
    $('#pwd_pin').keyup(function(){
        var pwd_pin = $.trim($('#pwd_pin').val()),
            regPin  = /^[0-9]{6}$/;
        if (pwd_pin != '' && regPin.test(pwd_pin)) { // 通过
            
            $('.tip1_hide').css('display','none');
            $('.tip1_img').css('display','inline-block');
            pinstatus = 1;
            Verified();
        }else{
            $('.tip1_hide').css('display','inline-block');
            $('.tip1_img').css('display','none');
            pinstatus = 0;
            Verified();
        }
    })
    $('#pwd1').keyup(function(){
        var pwd1 = $.trim($('#pwd1').val()),
            pwd2 = $.trim($('#pwd2').val());
            if (pwd1 == '') {
                $('.tip2_hide').css('display','inline-block');
                $('.tip2_img').css('display','none');
            }else if(pwd1.length >= 8 && pwd1.length <= 15) {
                if(pwd2 != ''){
                    if(pwd1 != pwd2){
                        //两次密码不一致
                        $('.tip2_hide').html('两次密码不一致!');
                        $('.tip2_hide').css('display','inline-block');
                        $('.tip2_img').css('display','none');
                        $('#next-step1').css({'background':'gray'});
                        $('#next-step1').removeAttr('onclick');
                        userpwd = 0;
                        Verified();
                    }else{
                        //两次密码一致
                        $('.tip2_hide').css('display','none');
                        $('.tip3_hide').css('display','none');
                        $('.tip2_img').css('display','inline-block');
                        $('.tip3_img').css('display','inline-block');
                        userpwd = 1;
                        Verified();
                        if(pinstatus == 1 && userpwd == 1) {
                            $('#next-step1').css({'background':'#3b9ce0'});
                            $('#next-step1').attr('onclick','nextStep()');
                        }else{
                            $('.next_step1').css({'background':'gray','color':'#fff'});
                            $('.next_step1').removeAttr('onclick');
                        }
                    }
                }else{
                    $('.tip2_hide').css('display','none');
                    $('.tip2_img').css('display','inline-block');
                }
            }else {
                $('.tip2_hide').css('display','inline-block');
                $('.tip2_img').css('display','none');
                $('#next-step1').css({'background':'gray'});
                $('#next-step1').removeAttr('onclick');
            }
    })
    $('#pwd2').keyup(function(){
        var pwd1 = $.trim($('#pwd1').val()),
            pwd2 = $.trim($('#pwd2').val());
            if (pwd1 == '' || pwd2 == '') {
                $('.tip3_hide').css('display','inline-block');
                $('.tip3_img').css('display','none');
            }else if(pwd2.length >= 8 && pwd2.length <= 15) {
                if(pwd1 != pwd2){
                    //两次密码不一致
                    $('.tip3_hide').html('两次密码不一致!');
                    $('.tip3_hide').css('display','inline-block');
                    $('.tip3_img').css('display','none');
                    $('#next-step1').css({'background':'gray'});
                    $('#next-step1').removeAttr('onclick');
                    userpwd = 0;
                    Verified();
                }else{
                    //两次密码一致
                    userpwd = 1;
                    Verified();
                    $('.tip3_hide').css('display','none');
                    $('.tip3_img').css('display','inline-block');
                    if(pinstatus == 1 && userpwd == 1) {
                        $('#next-step1').css({'background':'#3b9ce0'});
                        $('#next-step1').attr('onclick','nextStep()');
                    }else{
                        $('.next_step1').css({'background':'gray','color':'#fff'});
                        $('.next_step1').removeAttr('onclick');
                    }
                }
            }else{
                userpwd = 0;
                Verified();
                $('.tip3_hide').css('display','inline-block');
                $('.tip3_img').css('display','none');
                $('#next-step1').css({'background':'gray'});
                $('#next-step1').removeAttr('onclick');
            }
    })
    // 实名认证点击跳过
    $('#jump').click(function(){
        $('.tabItem2').hide();
        $('.tabItem3').show();
        $('.pin_tab > div').eq(2).attr('class','active').siblings('div').removeAttr('class');
//        var pin      = $.trim($('#pwd_pin').val()),
//            pwd1     = $.trim($('#pwd1').val()), //密码1
//            pwd2     = $.trim($('#pwd2').val()); //密码2
//        $.ajax({
//            url: '',
//            type: 'post',
//            data: {pin: pin,pwd1:pwd1,pwd2:pwd2},
//            dataType: json,
//            success: function(res){
//
//            }
//        })
    })
    // 实名认证页验证
    // 真实姓名
    $('#realName').blur(function(){
        var realName = $.trim($('#realName').val()),
            nameReg = /[\u4e00-\u9fa5]/;
        if(realName != '' && nameReg.test(realName)) {
            $('.tip4_hide').css('display','none');
            $('.tip4_img').css('display','inline-block');
            username = 1;
            Verified();
        }else{
            $('.tip4_hide').css('display','inline-block');
            $('.tip4_img').css('display','none');
            username = 0;
            Verified();
        }
    })
    // 手机号码
    $('#telNum').keyup(function(){
        var moblie = $.trim($('#telNum').val()),
            regTel = /^(13|14|15|17|18)\d{9}$/;
        if(moblie.length > 10){
           if(!(regTel.test(moblie))){
              $('.tip5_hide').html('请输入正确的手机号');
              $('.tip5_hide').css({"display":"inline-block"});
              $('.tip5_img').css({"display":"none"});
              $('#yzCode').removeAttr('onclick');
              $('#yzCode').css({'background':'#3e5565','color':'#fff'});
              telStatus=0;
              Verified();
           }else{
              $('.tip5_hide').css({"display":"none"});
              $('.tip5_img').css({"display":"inline-block"});
              $('#yzCode').attr('onclick',"settime(this)");
              $('#yzCode').css({'disabled':false});
              $('#yzCode').css({"background":"#3B9CE0","color":"#fff"});
//              $.ajax({
//                url: telurl,
//                type: 'post',
//                data: {tel:moblie},
//                headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
//                success: function(res){
//                    if(parseInt(res) === 1){
//                        $('.tip5_hide').html('该手机号已认证');
//                        $('.tip5_hide').css({"display":"inline-block"});
//                        $('#yzCode').css({'background':'#3e5565','color':'#fff'});
//                        $('#yzCode').removeAttr('onclick');
//                        $('#yzCode').css({'disabled':true});
//                        verifyStatus=0; //为了避免先输入验证码 然后换成别的手机号
//                        telStatus=0;
//                        Verified();
//                    }else{ //手机号未认证
//                        if((yanzhenma == 0) || (firsttel != moblie)){ //倒计时已结束 或者 用户填完手机验证码又修改了手机号
//                            clearInterval(t);
//                            $('#yzCode').attr("value", '获取验证码');
//                            $('#yzCode').css({"background":"#3B9CE0","color":"#fff"});
//                            $('.tip5_hide').css({"display":"none"});
//                            $('.tip5_img').css({"display":"inline-block"});
//                            telStatus=1;
//                            $('#yzCode').attr('onclick',"settime(this)");
//                            $('#yzCode').css({'disabled':false});
//                            verifyStatus=0; //为了避免先输入验证码 然后换成别的手机号
//                        }
//                        Verified();
//                   }
//                 }
//              });
           }
       }else{
          $('.tip5_hide').html('请输入正确的手机号');
          $('.tip5_hide').css({"display":"inline-block"});
          telStatus=0;
          Verified();
       }   
    });
    // 点击获取验证码按钮
    $('#yzCode').click(function(){   //判断验证码
        var moblie = $.trim($('#telNum').val());
        if(!(/^(13|14|15|17|18)\d{9}$/.test(moblie))){
           $('.tip6_hide').html('请输入正确的手机号');
           $('.tip6_hide').css({"display":"inline-block"});
           $('.yz_time').css({'background':'#3e5565','color':'#91a1ad'});
        }else{
            var usercode=$.trim($('#yznum').val());
            //填写了验证码
            if(usercode){
                if(usercode.length > 5){
                    $.ajax({
                        url: checkverifycode,
                        type: 'post',
                        data: {code:usercode},
                        dataType:'json',
                        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                        success: function(res){
                            if(parseInt(res.status) == 1){ //正确
                                $('.tip6_hide').css({"display":"none"});
                                verifyStatus=1;
                                Verified();
                            }else if(parseInt(res.status) == 0){
                                $('.tip6_hide').html('请输入正确的验证码');
                                $('.tip6_hide').css({"display":"inline-block"});
                                verifyStatus=0;
                                Verified();
                            }
                        }
                    });
                    verifyStatus = 1;
                    Verified();
                }else{
                    $('.tip6_hide').html('请输入正确的验证码');
                    $('.tip6_hide').css({"display":"inline-block"});
                    verifyStatus=0;
                    Verified();
                }
            }else{ //未填写验证码
                $('.tip6_hide').html('请输入验证码');
                $('.tip6_hide').css({"display":"inline-block"});
                verifyStatus=0;
                Verified();
            }
        }
    });
    // 输入验证码
    $('#yznum').keyup(function(){ // 输入的验证码与发送到手机号码的进行对比
        var yznum = $.trim($('#yznum').val());
        if(yznum == ''){
            $('.tip6_hide').css('display','inline-block');
            $('.tip6_img').css('display','none');
            verifyStatus = 0;
            Verified();
        }else if(yznum.length > 5){
            $.ajax({
                url: checkverifycode,
                type: 'post',
                data: {code:yznum},
                dataType:'json',
                headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                success: function(res){
                    if(parseInt(res.status) == 1){ //正确
                        $('.tip6_hide').css('display','none');
                        $('.tip6_img').css('display','inline-block');
                        verifyStatus=1;
                        Verified();
                    }else if(parseInt(res.status) == 0){
                        $('.tip6_hide').html('请输入正确的验证码');
                        $('.tip6_hide').css({"display":"inline-block"});
                        verifyStatus=0;
                        Verified();
                    }
                }
            });
            verifyStatus = 1;
            Verified();
        }else{
            $('.tip6_hide').html('请输入正确的验证码');
            $('.tip6_hide').css({"display":"inline-block"});
            verifyStatus=0;
            Verified();
        }
    })
    // 邮箱
    $('#emailNum').blur(function(){
        var emailNum = $.trim($('#emailNum').val()),
            emailReg = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
        if(emailNum != '' && emailReg.test(emailNum)) {
            $('.tip7_hide').css('display','none');
            $('.tip7_img').css('display','inline-block');
            emailstatus = 1;
            Verified();
        }else {
            $('.tip7_hide').css('display','inline-block');
            $('.tip7_img').css('display','none');
            emailstatus = 0;
            Verified();
        }
    })
    // 正面身份证
    $("#uploadzhengForm").on('submit', function(e){
        e.preventDefault();
        $.ajax({
            url: imgurl,
            type: "POST",
            data:  new FormData(this),
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            contentType: false,
            cache: false,
            processData: false,
            // 显示加载图片
            beforeSend: function () {
                $('.loading-shadow').addClass('active');
            },
            success: function (data) {
                if(data.status == 1) {
                    $('#img1').attr('src', data.msg);
                    $('#idcardimg').attr('value', data.msg);
                    zcardstatus = 1;
                    Verified();
                }else{
                    layer.tips(data.msg,'#img1',{time:1000});
                    zcardstatus = 0;
                    Verified();
                }
            },
            error: function(){
                zcardstatus = 0;
                Verified();
            }
        });
    });
    // 认证页面上传正面身份证
    var frontBox = document.querySelector("#frontBox");
    var front_icon = document.querySelector("#front_icon");
    frontBox.onclick = function(){
        front_icon.click();     //file1触发事件
    }
    var myFile;
    front_icon.onchange = function(){     //当事件改变的时候触发
        // 获取上传文件的大小
        var file_size=parseInt((document.getElementById('front_icon').files[0].size)/1024/1024)
        if(file_size <= 2) {
            $("#uploadzhengForm").trigger('submit');
        }else{
            layer.tips('图片大小不能超过2M!','#img1',{time:1000});
        }
    }
    // 反面身份证
    $("#uploadfanForm").on('submit', function(e){
        e.preventDefault();
        $.ajax({
            url: imgurl,
            type: "POST",
            data:  new FormData(this),
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            contentType: false,
            cache: false,
            processData: false,
            // 显示加载图片
            beforeSend: function () {
                $('.loading-shadow').addClass('active');
            },
            success: function (data) {
                if(data.status == 1){
                    $('#img2').attr('src',data.msg);
                    $('#idcardimgf').attr('value',data.msg);
                    fcardstatus = 1;
                    Verified();
                }else{
                    layer.tips(data.msg,'#img2',{time:1000});
                    fcardstatus = 0;
                    Verified();
                }
            },
            error: function(){
                fcardstatus = 0;
                Verified();
            }
        });
    });
    //认证页面上传反面身份证
    var backBox = document.querySelector("#backBox");
    var back_icon = document.querySelector("#back_icon")   
    backBox.onclick = function(){
        back_icon.click();     //file1触发事件
    }
    var myFile;
    back_icon.onchange = function(){     //当事件改变的时候触发
        var file_size=parseInt((document.getElementById('back_icon').files[0].size)/1024/1024)
        if(file_size <= 2) {
            $("#uploadfanForm").trigger('submit');
        }else{
            layer.tips('图片大小不能超过2M!','#img2',{time:1000});
        }
    }
    // 居住地址
    $('#addr').blur(function(){
        var addr = $.trim($('#addr').val());
        if(addr != '') {
            $('.tip8_hide').css('display','none');
            $('.tip8_img').css('display','inline-block');
            addrstatus = 1;
            Verified();
        }else {
            $('.tip8_hide').css('display','inline-block');
            $('.tip8_img').css('display','none');
            addrstatus = 0;
            Verified();
        }
    })
    // 居住证主页
    $("#uploadhomeForm").on('submit', function(e){
        e.preventDefault();
        $.ajax({
            url: imgurl,
            type: "POST",
            data:  new FormData(this),
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            contentType: false,
            cache: false,
            processData: false,
            // 显示加载图片
            beforeSend: function () {
                $('.loading-shadow').addClass('active');
            },
            success: function (data) {
                if(data.status == 1) {
                    $('#homeimg1').attr('src', data.msg);
                    $('#homeImg').attr('value', data.msg);
                    homepagestatus = 1;
                    Verified();
                }else{
                    layer.tips(data.msg,'#homeimg1',{time:1000});
                    homepagestatus = 0;
                    Verified();
                }
            },
            error: function(){
                homepagestatus = 0;
                Verified();
            }
        });
    });
    // 居住证主页
    var homePage = document.querySelector("#homePage");
    var home_icon = document.querySelector("#home_icon");
    homePage.onclick = function(){
        home_icon.click();     //file1触发事件
    }
    var myFile;
    home_icon.onchange = function(){     //当事件改变的时候触发
        // 获取上传文件的大小
        var file_size=parseInt((document.getElementById('home_icon').files[0].size)/1024/1024)
        if(file_size <= 2) {
            $("#uploadhomeForm").trigger('submit');
        }else{
            layer.tips('图片大小不能超过2M!','#img1',{time:1000});
        }
    }
    // 居住证副页
    $("#uploadfuhomeForm").on('submit', function(e){
        e.preventDefault();
        $.ajax({
            url: imgurl,
            type: "POST",
            data:  new FormData(this),
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            contentType: false,
            cache: false,
            processData: false,
            // 显示加载图片
            beforeSend: function () {
                $('.loading-shadow').addClass('active');
            },
            success: function (data) {
                if(data.status == 1){
                    $('#homeimg2').attr('src',data.msg);
                    $('#fuHomeImg').attr('value',data.msg);
                    fupagestatus = 1;
                    Verified();
                }else{
                    layer.tips(data.msg,'#homeimg2',{time:1000});
                    fupagestatus = 0;
                    Verified();
                }
            },
            error: function(){
                fupagestatus = 0;
                Verified();
            }
        });
    });
    //上传居住证副页
    var fuHomePage = document.querySelector("#fuHomePage");
    var fuHome_icon = document.querySelector("#fuHome_icon")   
    fuHomePage.onclick = function(){
        fuHome_icon.click();     //file1触发事件
    }
    var myFile;
    fuHome_icon.onchange = function(){     //当事件改变的时候触发
        var file_size=parseInt((document.getElementById('fuHome_icon').files[0].size)/1024/1024)
        if(file_size <= 2) {
            $("#uploadfuhomeForm").trigger('submit');
        }else{
            layer.tips('图片大小不能超过2M!','#homeimg2',{time:1000});
        }
    }
    // 激活成功
    $('.pin_sure').click(function(){
        // 获取前面充值密码和实名认证的所有信息
        var pin        = $.trim($("#pwd_pin").val()), //PIN码
            pwd1       = $.trim($("#pwd1").val()), // 密码
            realName   = $.trim($('#realName').val()), //用户名
            phone      = $.trim($('#telNum').val()), //手机号
            emailNum   = $.trim($('#emailNum').val()), //邮箱
            addr       = $.trim($('#addr').val()), //居住地址
            idcardimg  = $("#idcardimg").val(),//身份证正面
            idcardimgf = $("#idcardimgf").val(),//身份证反面
            homeimg    = $("#homeImg").val(),//居住证
            fuhomeimg  = $("#fuHomeImg").val();//居住证副页

        $.ajax({
            url: activate,
            type: 'post',
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            data: {pin:pin,pwd:pwd1,realName:realName,phone:phone,email:emailNum,addr:addr,idcardimg:idcardimg,idcardimgf:idcardimgf,homeimg:homeimg,fuhomeimg:fuhomeimg},
//            dataType:'json',
            success:function(res){
                if(res == 1){
                    alert('激活成功');
                    window.location.href = loginurl;
                }else if(res == 2){
                    alert('请先登录');
                }else{
                    alert('输入有误，请重新填写');
                }
            }
            
        });
    })
})
// PIN账号第一次登录重置密码，点击下一步跳到实名认证一页
function nextStep(){
    var pin        = $.trim($("#pwd_pin").val()), //PIN码
        pwd1       = $.trim($("#pwd1").val()), // 密码1
        pwd2       = $.trim($("#pwd2").val()); // 密码2
    
    if(pwd1 != pwd2){
        alert('两次密码输入不一致');
        return false;
    }    
    $.ajax({
        url:checkpin,
        type:'post',
        data:{pin:pin},
        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
//        dataType:'json',
        success:function(data){
            if(data == 0){
                alert('PIN账号不存在，请重新输入');
                return false;
            }else if(data == 2){
                alert('请先登录');
                return false;
            }else{
                $('.tabItem1').hide();
                $('.tabItem2').show();
                $('.pin_tab > div').eq(1).attr('class','active').siblings('div').removeAttr('class');
            }
        }
    });  
}

// PIN实名认证，填完信息后跳到激活完成一页
function nextStep2(){
    $('.tabItem2').hide();
    $('.tabItem3').show();
    $('.pin_tab > div').eq(2).attr('class','active').siblings('div').removeAttr('class');
}
//发送手机验证码
function settime(obj) {
    var telNum=$.trim($('#telNum').val());
    if(!(/^(13|14|15|17|18)\d{9}$/.test(telNum))){ //手机号不正确
        $('.tip6_hide').html('请输入正确的手机号');
        $('.tip6_hide').css({"display":"inline-block"});
    }else{
        $('#yzCode').removeAttr("onclick");
        $('#yzCode').css({'disabled':true});
        firsttel = telNum; //记录发送验证码的手机号
        $.ajax({
            url:sendIdentify,
            type: 'post',
            data: {tel:telNum},
            dataType:'json',
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            success: function(res){
                if(parseInt(res.status) == 0){
                   $('.tip6_hide').html('发送失败,请重新发送');
                   $('.tip6_hide').css({"display":"inline-block"});
                   $('#yzCode').attr('onclick','settime(this)');
                }else if(parseInt(res.status) == 1){
                    yanzhenma = 1;
                    $('#yzCode').removeAttr('onclick');
                    $('#yzCode').css({'background':'#3e5565','color':'#fff'});
                    $('#yznum').focus();

                    window.code=res.code;
                    var countdown=60;
                    var validCode=true;
                    if(validCode){
                        validCode = false;
                        t=setInterval(function () {
                        countdown--;
                        $('#yzCode').html(countdown+"重新发送");
                        // obj.value = countdown+"s后重发";
                        $('#yzCode').removeAttr("onclick");
                        $('#yzCode').css({'disabled':true});
                        if (countdown == 0) {
                         yanzhenma = 0;
                         clearInterval(t);
                         $('#yzCode').html("重新发送")
                         // obj.value="重新发送";
                         $('#yzCode').css({'background':'#3B9CE0','color':'#fff'});
                         $('#yzCode').attr("onclick", 'settime(this)');
                         $('#yzCode').css({'disabled':false});
                         validCode=true;
                        }
                       },1000);
                    }
                }
            }
        });
    }
}
//实名认证
function  Verified() {
    if(telStatus == 0 && verifyStatus == 0 && emailstatus == 1 && addrstatus == 1 && zcardstatus == 1 && fcardstatus == 1 && homepagestatus == 1 && fupagestatus == 1){
        //全部成功
        $('.next_step2').css({'background':'#3b9ce0','color':'#fff'});
        $('.next_step2').attr('onclick','nextStep2()');
    }else{
        $('.next_step2').css({'background':'gray','color':'#fff'});
        $('.next_step2').removeAttr('onclick');
    }
}
